package com.example.simpleprefencedemo;

import android.content.Context;
import android.content.SharedPreferences;
import android.view.View;
import android.widget.EditText;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;

public class MainActivity extends AppCompatActivity {
    public static final int MODE= Context.MODE_APPEND;
    public static final String PREFERENCE_NAME="Setting";
    EditText edt1;
    EditText edt2;
    EditText edt3;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
         edt1 =(EditText) findViewById(R.id.editTextTextPersonName);
         edt2 =(EditText) findViewById(R.id.editTextTextPersonName2);
         edt3 =(EditText) findViewById(R.id.editTextTextPersonName3);
    }

    @Override
    protected void onStart() {
        super.onStart();
        loadSharedPreference();
    }
    private void loadSharedPreference(){
        SharedPreferences sh=getSharedPreferences(PREFERENCE_NAME,MODE);
        String name = sh.getString("name","zs");
        edt1.setText(name);
        String age = sh.getString("age","18");
        edt2.setText(age);
        String height = sh.getString("height","1.81");
        edt3.setText(height);
    }
    @Override
    protected void onStop() {
        super.onStop();

        saveSharedPreference("name",edt1.getText().toString());
        saveSharedPreference("age",edt2.getText().toString());
        saveSharedPreference("height",edt3.getText().toString());
    }
    private void saveSharedPreference(String key,String value){
        SharedPreferences sh=getSharedPreferences(PREFERENCE_NAME,MODE);
        SharedPreferences.Editor editor=sh.edit();
        editor.putString(key,value);
        editor.commit();
    }
}